<?php

namespace Evaluating\Controller;

use Common\Controller\BaseController;
use Think\Page;

class ReviewController extends BaseController
{
    public function index()
    {
        $task_type = 2;
        $model = D('Task');
        $sp = $_GET['p'];
        if (empty($sp)) {
            $sp = 1;
        }
        $search_key = $_POST['search_key'];
        $status = 1;

        $user_id = $this->current_user['id'];
        $list = $model->get_list($search_key, $status, $task_type, $sp, C('PAGE_SIZE'));

        $page = new Page($list[1], C('PAGE_SIZE'));

        $this->assign('total_rows', $page->totalRows);
        $this->assign('_page', $page);
        $this->assign('page_link', $page->show());
        $this->assign('task_list', $list[0]);

        foreach ($_POST as $key => $val) {
            $page->parameter[$key] = urlencode($val);
        }

        $this->display();
    }

    public function detail()
    {
        $taskid = $_GET['taskid'];
        $s_status = $_GET['search_status'];
        if (empty($taskid)) {
            $taskid = $_POST['taskid'];
        }
        $search_key = $_POST['search_key'];
        $search_status = $_POST['search_status'];
        if ($search_status != '') {
            $status = explode(",", $search_status);
        } else {
            if (!empty($s_status)) {
                $status = $s_status;
            }
        }
        $model = D('Task');
        $task = $model->get($taskid);
        $this->assign('task', $task);

        $model = D('VUserTask');
        $list = $model->get_list($user_id, $search_key, $status, $task_type, $taskid, $sp, 0);

        $page = new Page($list[1], C('PAGE_SIZE'));
        $this->assign('user_task_list', $list[0]);
        $this->assign('_page', $page);

        foreach ($_POST as $key => $val) {
            $page->parameter[$key] = urlencode($val);
        }

        $this->display();
    }

    public function edit()
    {
        $id = $_GET['id'];
        $usertaskquotamodel = D('VUserTaskQuota');
        $user_task_quota = $usertaskquotamodel->get($id);
        $this->assign('user_task_quota', $user_task_quota);

        $usertaskmodel = D('VUserTask');
        $user_task = $usertaskmodel->get($user_task_quota['user_task_id']);

        $this->assign('user_task', $user_task);

        $usertaskannexmodel = D('UserTaskAnnex');
        $condition = array();
        $condition['user_task_quota_id'] = array('eq', $id);
        $annex_list = $usertaskannexmodel->where($condition)->order('id asc')->select();
        $this->assign('annex_list', $annex_list);
        $this->display();
    }

    public function quota()
    {
        $id = $_GET['id'];
        $usertaskmodel = D('VUserTask');
        $user_task = $usertaskmodel->get($id);
        $this->assign('user_task', $user_task);

        $usertaskquotamodel = D('VUserTaskQuota');
        $user_task_quota_list = $usertaskquotamodel->get_list($user_task['id'], null);

        $this->assign('user_task_quota_list', $user_task_quota_list);
        $this->display();
    }

    public function operate()
    {
        $model = D('UserTask');

        $action_type = $_POST['action_type'];
        $user_task_id = $_POST['user_task_id'];

        $data = array('status' => $action_type);
        $model->where('id=%d', $user_task_id)->setField($data);

        $uset_task = $model->where('id=%d', $user_task_id)->find();

        redirect('/evaluating/review/detail?taskid=' . $uset_task['task_id'] . '&search_status=3');
    }

}
